﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Activation;
using JM.JCY.Proxy;
using JM.JCY.BusinessEntity;
using Newtonsoft.Json;
using NCS.IConnect.ApplicationContexts;
using JM.JCY.WcfService.Contracts;


namespace JM.JCY.WcfService
{
    // 注意: 使用“重构”菜单上的“重命名”命令，可以同时更改代码、svc 和配置文件中的类名“NewsFeed”。
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class NewsFeed : INewsFeed
    {
        public string GetFeedPostLastestUpdateTime(int feedPostId)
        {
            DateTime dt = new DateTime();
            using (NewsfeedProxy proxy = new NewsfeedProxy())
            {
                dt = proxy.GetFeedPostLastestUpdateTime(feedPostId);
            }
            return dt.ToString("yyyy-MM-dd HH:mm:ss");
        }

        public string GetFeedPost(int staffId, string appType,int appId, int pageNum, string excludePostId)
        {
            if (excludePostId.Trim() != string.Empty && excludePostId.IndexOf("|") != -1)
            {
                excludePostId = excludePostId.Substring(0, excludePostId.LastIndexOf('|'));
            }
            using (NewsfeedProxy proxy = new NewsfeedProxy())
            {
                //int endId = pageNum * JCY.Common.JCYConstant.NewsfeedParams.DefaultNewsfeedNum;
                NewsfeedDataSet.T_FeedPostDataTable newsfeeds = proxy.GetFeedPost(appType, appId, excludePostId);

                int maxId = 0;
                if (newsfeeds.Rows.Count > 0)
                {
                    maxId = newsfeeds[0].FeedPostId;
                }
                var result = from feed in newsfeeds
                             //where feed.id<pageNum
                             select new
                             {
                                 maxId = maxId,
                                 postCount = newsfeeds.Rows.Count,
                                 id = feed.FeedPostId,
                                 staff = new
                                 {
                                     id = feed.StaffId,
                                     name = feed.StaffName,
                                     imgUrl = feed.PhotoImagePath
                                 },
                                 content = feed.PostContent,
                                 timestamp = feed.CREATED_TIME,
                                 creator = feed.StaffId,
                                 lastestUpdatedTime = feed.LAST_UPDATED_TIME.ToString("yyyy-MM-dd HH:mm:ss")
                             };
                return JsonConvert.SerializeObject(result.ToList());
            }
        }

        public string GetMoreFeedPost(int staffId, string appType, int appId, string excludePostId)
        {
            if (excludePostId.Trim() != string.Empty && excludePostId.IndexOf("|") != -1)
            {
                excludePostId = excludePostId.Substring(0, excludePostId.LastIndexOf('|'));
            }
            using (NewsfeedProxy proxy = new NewsfeedProxy())
            {
                //int endId = pageNum * JCY.Common.JCYConstant.NewsfeedParams.DefaultNewsfeedNum;
                NewsfeedDataSet.T_FeedPostDataTable newsfeeds = proxy.GetMoreFeedPost(appType, appId, excludePostId);

                int maxId = 0;
                if (newsfeeds.Rows.Count > 0)
                {
                    maxId = newsfeeds[0].FeedPostId;
                }
                var result = from feed in newsfeeds
                             //where feed.id<pageNum
                             select new
                             {
                                 maxId = maxId,
                                 postCount = feed.totalCount,
                                 id = feed.FeedPostId,
                                 staff = new
                                 {
                                     id = feed.StaffId,
                                     name = feed.StaffName,
                                     imgUrl = feed.PhotoImagePath
                                 },
                                 content = feed.PostContent,
                                 timestamp = feed.CREATED_TIME,
                                 creator = feed.StaffId,
                                 lastestUpdatedTime = feed.LAST_UPDATED_TIME.ToString("yyyy-MM-dd HH:mm:ss")
                             };
                return JsonConvert.SerializeObject(result.ToList());
            }
        }

        //used
        public string InsertComment(int staffId, int feedPostId, string content)
        {
            using (NewsfeedProxy proxy = new NewsfeedProxy())
            {
                NewsfeedDataSet ds = new NewsfeedDataSet();
                NewsfeedDataSet.T_FeedCommentDataTable dt = ds.T_FeedComment;
                NewsfeedDataSet.T_FeedCommentRow feedcommentRow = dt.NewT_FeedCommentRow();

                feedcommentRow.Content = content;
                feedcommentRow.FeedPostId = feedPostId;
                feedcommentRow.StaffId = staffId;

                feedcommentRow.CREATED_BY = staffId.ToString();
                feedcommentRow.CREATED_TIME = DateTime.Now;
                feedcommentRow.IsDeleted = false;
                feedcommentRow.TRANSACTION_ID = "1";
                feedcommentRow.VERSION_NO = 1;

                ds.T_FeedComment.AddT_FeedCommentRow(feedcommentRow);
                NewsfeedDataSet.T_FeedCommentDataTable dtFeedComment = proxy.InsertComment(ds);

                //return the instance of this comment

                var result = from comment in dtFeedComment
                             select new //Anonymous object
                             {
                                 actualCount = comment.actualCount,
                                 hasMoreComments = comment.isThereMore,
                                 commentId = comment.FeedCommentId,
                                 staff = new
                                 {
                                     id = comment.StaffId,
                                     name = comment.StaffName,
                                     imgUrl = comment.PhotoImagePath,
                                     postOwner = comment.postOwner
                                 },
                                 content = comment.Content,
                                 timestamp = comment.CREATED_TIME,
                                 postId = comment.FeedPostId
                             };
                return JsonConvert.SerializeObject(result);

            }
        }
        //used
        public string InsertCommentWithObjId(int staffId, int objId, string content)
        {
            using (NewsfeedProxy proxy = new NewsfeedProxy())
            {
                NewsfeedDataSet ds = new NewsfeedDataSet();
                NewsfeedDataSet.T_FeedPostDataTable feedPost = ds.T_FeedPost;
                feedPost = proxy.GetFeedPostByObjectId(objId);
                if (feedPost.Rows.Count == 0)
                {
                    NewsfeedDataSet.T_FeedPostRow feedpostRow = ds.T_FeedPost.NewT_FeedPostRow();
                    NewsfeedDataSet.T_UserInfoDataTable userInfo = new NewsfeedDataSet.T_UserInfoDataTable();
                    userInfo = proxy.GetUserInfoEntity(staffId);
                    var userResult = from staff in userInfo
                                     select new
                                     {
                                         userId = staff.ID,
                                         userName = staff.UserName,
                                         userImageUrl = staff.PhotoImagePath
                                     };
                    feedpostRow.StaffId = staffId;
                    feedpostRow.ObjectId = objId;
                    feedpostRow.PostContent = content;
                    feedpostRow.AppType = "SPObject";
                    feedpostRow.AppId = objId;
                    feedpostRow.IsDeleted = false;
                    feedpostRow.LAST_UPDATED_BY = staffId.ToString();
                    feedpostRow.LAST_UPDATED_TIME = DateTime.Now;
                    feedpostRow.CREATED_BY = staffId.ToString();
                    feedpostRow.CREATED_TIME = DateTime.Now;
                    feedpostRow.VERSION_NO = 1;
                    feedpostRow.TRANSACTION_ID = "1";
                    ds.T_FeedPost.AddT_FeedPostRow(feedpostRow);
                    proxy.InsertFeedPost(ds);
                    feedPost = proxy.GetFeedPostByObjectId(objId);
                    feedPost[0].StaffName = userResult.SingleOrDefault().userName;
                    feedPost[0].PhotoImagePath = userResult.SingleOrDefault().userImageUrl;
                }
                NewsfeedDataSet.T_FeedCommentDataTable dt = ds.T_FeedComment;
                NewsfeedDataSet.T_FeedCommentRow feedcommentRow = dt.NewT_FeedCommentRow();

                feedcommentRow.Content = content;
                feedcommentRow.FeedPostId = feedPost[0].FeedPostId;
                feedcommentRow.StaffId = staffId;

                feedcommentRow.CREATED_BY = staffId.ToString();
                feedcommentRow.CREATED_TIME = DateTime.Now;
                feedcommentRow.IsDeleted = false;
                feedcommentRow.TRANSACTION_ID = "1";
                feedcommentRow.VERSION_NO = 1;

                ds.T_FeedComment.AddT_FeedCommentRow(feedcommentRow);
                NewsfeedDataSet.T_FeedCommentDataTable dtFeedComment = proxy.InsertComment(ds);

                //return the instance of this comment

                var result = from comment in dtFeedComment
                             orderby dtFeedComment.CREATED_TIMEColumn
                             select new //Anonymous object
                             {
                                 actualCount = comment.actualCount,
                                 hasMoreComments = comment.isThereMore,
                                 commentId = comment.FeedCommentId,
                                 staff = new
                                 {
                                     id = comment.StaffId,
                                     name = comment.StaffName,
                                     imgUrl = comment.PhotoImagePath,
                                     postOwner = comment.postOwner
                                 },
                                 content = comment.Content,
                                 timestamp = comment.CREATED_TIME,
                                 postId = comment.FeedPostId
                             };
                return JsonConvert.SerializeObject(result);

            }
        }

        public string GetPostComment(int feedPostId)
        {
            NewsfeedDataSet.T_FeedCommentDataTable comments = new NewsfeedDataSet.T_FeedCommentDataTable();
            using (NewsfeedProxy proxy = new NewsfeedProxy())
            {
                comments = proxy.GetPostCommentByPostId(feedPostId, 3);
            }
            var result = from comment in comments
                         select new
                         {
                             actualCount = comment.actualCount,
                             hasMoreComments = comment.isThereMore,
                             commentId = comment.FeedCommentId,
                             staff = new
                             {
                                 id = comment.StaffId,
                                 name = comment.StaffName,
                                 imgUrl = comment.PhotoImagePath,
                                 postOwner = comment.postOwner
                             },
                             content = comment.Content,
                             timestamp = comment.CREATED_TIME,
                             postId = feedPostId
                         };
            return JsonConvert.SerializeObject(result);
        }

        public string GetMoreCommentByFeedpost(int feedPostId)
        {
            NewsfeedDataSet.T_FeedCommentDataTable comments = new NewsfeedDataSet.T_FeedCommentDataTable();
            using (NewsfeedProxy proxy = new NewsfeedProxy())
            {
                comments = proxy.GetPostCommentByPostId(feedPostId, 30);
            }

            var result = from comment in comments
                         select new //Anonymous object
                         {
                             actualCount = comment.actualCount,
                             hasMoreComments = comment.isThereMore,
                             commentId = comment.FeedCommentId,
                             staff = new
                             {
                                 id = comment.StaffId,
                                 name = comment.StaffName,
                                 imgUrl = comment.PhotoImagePath,
                                 postOwner = comment.postOwner
                             },
                             content = comment.Content,
                             timestamp = comment.CREATED_TIME,
                             postId = feedPostId
                         };
            return JsonConvert.SerializeObject(result);
        }

        //used
        public string GetMoreCommentsBySPObj(int objectId)
        {
            NewsfeedDataSet.T_FeedCommentDataTable dt = new NewsfeedDataSet.T_FeedCommentDataTable();
             using (NewsfeedProxy proxy = new NewsfeedProxy())
            {
                dt = proxy.GetMoreCommentsByObj(objectId, JM.JCY.Common.JCYConstant.NewsfeedParams.DefaultPostContentNum);
             }
             var result = from comment in dt
                          select new //Anonymous object
                          {
                              comment.Content,
                              commentId=comment.FeedCommentId,
                              staff = new
                              {
                                  name = comment.StaffName,
                                  imageUrl = comment.PhotoImagePath,
                                  id=comment.StaffId,
                                  postOwner = comment.postOwner
                              },
                              CREATED_TIME=comment.CREATED_TIME.ToString("yyyy-MM-dd HH:mm:ss"),
                              actualCount = comment.actualCount
                          };
             return JsonConvert.SerializeObject(result);
        }

       public string InsertFeedPost(int staffId,string postContent,string appType,int appId)
        {
            using (NewsfeedProxy proxy = new NewsfeedProxy())
            {
                NewsfeedDataSet ds = new NewsfeedDataSet();
                NewsfeedDataSet.T_FeedPostRow feedpostRow = ds.T_FeedPost.NewT_FeedPostRow();
                NewsfeedDataSet.T_UserInfoDataTable userInfo = new NewsfeedDataSet.T_UserInfoDataTable();
                userInfo = proxy.GetUserInfoEntity(staffId);
                var userResult = from staff in userInfo
                                 select new
                                 {
                                     userId = staff.ID,
                                     userName = staff.UserName,
                                     userImageUrl = staff.PhotoImagePath
                                 };
                feedpostRow.StaffId = staffId;
                feedpostRow.ObjectId = 0;
                feedpostRow.PostContent = postContent;
                feedpostRow.AppType = appType;
                feedpostRow.AppId = appId;
                feedpostRow.IsDeleted = false;
                feedpostRow.LAST_UPDATED_BY = staffId.ToString();
                feedpostRow.LAST_UPDATED_TIME = DateTime.Now;
                feedpostRow.CREATED_BY = staffId.ToString();
                feedpostRow.CREATED_TIME = DateTime.Now;
                feedpostRow.VERSION_NO = 1;
                feedpostRow.TRANSACTION_ID = "1";
                ds.T_FeedPost.AddT_FeedPostRow(feedpostRow);
                proxy.InsertFeedPost(ds);
                ds.T_FeedPost[0].StaffName = userResult.SingleOrDefault().userName;
                ds.T_FeedPost[0].PhotoImagePath = userResult.SingleOrDefault().userImageUrl;

                var newsfeedResult = from newsfeed in ds.T_FeedPost
                                     select new
                                     {
                                         id = newsfeed.FeedPostId,
                                         content = newsfeed.PostContent,
                                         timestamp = newsfeed.CREATED_TIME,
                                         creator = newsfeed.StaffId,
                                         staff = new
                                         {
                                             id = newsfeed.StaffId,
                                             name = newsfeed.StaffName,
                                             imgUrl = newsfeed.PhotoImagePath
                                         }
                                     };
                return JsonConvert.SerializeObject(newsfeedResult);
            }
        }

       public bool DeleteComment(int commentId)
       {
           using (NewsfeedProxy proxy = new NewsfeedProxy())
           {
               return proxy.DeletePostCommentById(commentId);
           }
       }

       public void DeleteFeedPost(int feedpostId)
       {
           using (NewsfeedProxy proxy = new NewsfeedProxy())
           {
               proxy.DeleteFeedPost(feedpostId);
           }
       }

       public string GetLatestFeedPostUpdateTime(string appType, int appId)
       {
           DateTime dt = new DateTime();
           using (NewsfeedProxy proxy = new NewsfeedProxy())
           {
               dt = proxy.GetLatestFeedPostUpdateTime(appType, appId);
           }
           return dt.ToString("yyyy-MM-dd HH:mm:ss");
       }
    }
}
